package com.gx.recruitment.controller;

import com.gx.recruitment.entity.Comp_job;
import com.gx.recruitment.entity.Comp_jobExample;
import com.gx.recruitment.service.CompJobService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CompJobController {
    @Resource
    private CompJobService compJobService;

    @PostMapping("/compjob/add")
    public Map<String, Object> insertJob(
            String jobname,
            String jobpay,
            String jobaddress,
            String jobedu,
            String jobexpr,
            String jobcompany,
            String jobdetail,
            String jobindustry,
            Integer compid) {
        Map<String, Object> map = new HashMap<>();
        Date jobdate = new java.sql.Date(new java.util.Date().getTime());
        System.out.println(jobdate);
        Comp_job comp_job = new Comp_job();
        comp_job.setJobName(jobname);
        comp_job.setJobPay(jobpay);
        comp_job.setJobAddress(jobaddress);
        comp_job.setJobEdu(jobedu);
        comp_job.setJobExpr(jobexpr);
        comp_job.setJobDate(jobdate);
        comp_job.setJobCompany(jobcompany);
        comp_job.setJobIndustry(jobindustry);
        comp_job.setJobDetail(jobdetail);
        comp_job.setCompId(compid);

        int res = compJobService.insertJob(comp_job);
        if (res == 0) {
            map.put("msg", "success");
            return map;
        } else {
            map.put("msg", "fail");
            return map;
        }
    }

    @GetMapping("/compjob/getjobs")
    public List<Comp_job> getJobs() {
        Comp_jobExample example = new Comp_jobExample();
        List<Comp_job> jobs = compJobService.selectJob(example);
        return jobs;
    }

    @PostMapping("/compjob/getjobsBycompid")
    public List<Comp_job> getjobsBycompid(Integer compid) {
        Comp_jobExample example = new Comp_jobExample();
        Comp_jobExample.Criteria cri = example.createCriteria();
        cri.andCompIdEqualTo(compid);
        List<Comp_job> jobs = compJobService.selectJobByCompId(example);
        return jobs;
    }

    @PostMapping("/compjob/deleteone")
    public Map<String, Object> deleteJob(Integer jobid) {
        System.out.println(jobid);
        Map<String, Object> map = new HashMap<>();
        int res = compJobService.deleteByJobId(jobid);
        if (res == 0) {
            map.put("msg", "success");
            return map;
        } else {
            map.put("msg", "fail");
            return map;
        }
    }

    @PostMapping("/compjob/search")
    public List<Comp_job> selectJobByJobOrComp(String select, String input) {
        Comp_jobExample example = new Comp_jobExample();
        Comp_jobExample.Criteria cri = example.createCriteria();
        System.out.println(select);
        System.out.println(input);
        if (select.equals("职位")) {
            cri.andJobNameLike("%" + input + "%");
        } else {
            cri.andJobCompanyLike("%" + input + "%");
        }
        List<Comp_job> lists = compJobService.selectJobByJobOrComp(example);
        System.out.println(lists);
        return lists;
    }

    @PostMapping("/compjob/searchByMix")
    public List<Comp_job> searchByMix(String jobaddress, String jobindustry, String jobpay) {
        System.out.println(jobaddress + jobindustry + jobpay);
        Comp_jobExample example = new Comp_jobExample();
        Comp_jobExample.Criteria criteria = example.createCriteria();
        criteria.andJobAddressLike("%"+jobaddress+"%");
        criteria.andJobIndustryLike("%"+jobindustry+"%");
        criteria.andJobPayLike("%"+jobpay+"%");
        List<Comp_job> lists = compJobService.selectByExample(example);
        System.out.println(lists);
        return lists;
    }
}